/*

* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Smart-Shop开源商城系统/ Java商城-首选电商平台系统 请务必保留此注释头信息
* |     开源地址: https://gitee.com/ningbo-qishan/gruul-mall
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     可正常分享和学习源码,未经授权 不可商用！
* |     商业版中台购买/开源版商业授权 联系技术客服
* |     官网:  https://www.73app.cn/
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Copyright (c) 2016-2999 宁波启山智软科技有限公司
* |     版权所有 ,侵权必究！
* ------------------------------------------------------------------------

*/
package com.medusa.gruul.finance.controller.manage;


import com.medusa.gruul.common.core.annotation.EscapeLogin;
import com.medusa.gruul.common.core.util.PageUtils;
import com.medusa.gruul.common.core.util.Result;
import com.medusa.gruul.finance.api.entity.FinanceTradeDetail;
import com.medusa.gruul.finance.api.entity.FinanceWithdrawalRecord;
import com.medusa.gruul.finance.api.enums.AccountTypeEnum;
import com.medusa.gruul.finance.api.enums.ChannelTypeEnum;
import com.medusa.gruul.finance.model.*;
import com.medusa.gruul.finance.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.Arrays;

/**
 * <p>
 * PC端佣金规则设置 前端控制器
 * </p>
 *
 * @author alan
 * @since 2020 -02-22
 */
@Slf4j
@RestController
@RequestMapping("/manage")
@Api(tags = "PC端佣金")
public class BrokerageController {

    @Resource
    private IFinanceTradeDetailService financeTradeDetailService;
    @Resource
    private IFinanceWithdrawalRecordService financeWithdrawalRecordService;

    /**
     * Gets overview.
     *
     * @param startTime the start time
     * @param endTime   the end time
     * @return the overview
     */
    @ApiOperation("店铺实时概况")
    @GetMapping("/overview")
    public Result<ShopOverviewVo> getOverview(
            @RequestParam(value = "startTime", required = false) String startTime,
            @RequestParam(value = "endTime", required = false) String endTime) {
        ShopOverviewVo vo = financeTradeDetailService.getOverview(startTime, endTime);
        return Result.ok(vo);
    }

    /**
     * Gets trade overview.
     *
     * @param dto the dto
     * @return the trade overview
     */
    @ApiOperation("对账单概览")
    @GetMapping("/trade")
    public Result<TradeOverviewVo> getTradeOverview(SearchTradeDetailDto dto) {
        TradeOverviewVo vo = financeTradeDetailService.getTradeOverview(dto);
        return Result.ok(vo);
    }


    /**
     * Gets trade detail.
     *
     * @param dto the dto
     * @return the trade detail
     */
    @ApiOperation("对账单流水记录")
    @GetMapping("/trade/detail")
    public Result<PageUtils<FinanceTradeDetail>> getTradeDetail(SearchTradeDetailDto dto) {
        PageUtils pageUtils = financeTradeDetailService.getTradeDetailPage(dto);
        return Result.ok(pageUtils);
    }


//=============================== 佣金=======================================================

    /**
     * Gets withdrawal record page.
     *
     * @param dto the dto
     * @return the withdrawal record page
     */
    @ApiOperation("申请提现列表")
    @GetMapping("/withdrawal")
    public Result<PageUtils<FinanceWithdrawalRecord>> getWithdrawalRecordPage(SearchWithdrawalRecordDto dto) {
        PageUtils pageUtils = financeWithdrawalRecordService.searchWithdrawalRecordPage(dto);
        return Result.ok(pageUtils);
    }



    /**
     * Gets trade detail info.
     *
     * @param id the id
     * @return the trade detail info
     */
    @ApiOperation("单条提现详情")
    @GetMapping("/withdrawal/info/{id}")
    public Result<FinanceWithdrawalRecord> getTradeDetailInfo(@PathVariable(value = "id") @NotNull Long id) {
        FinanceWithdrawalRecord withdrawalRecord = financeWithdrawalRecordService.getById(id);
        return Result.ok(withdrawalRecord);
    }

    /**
     * Pass withdrawal result.
     *
     * @param id      the id
     * @param tradeNo the trade no
     * @return the result
     */
    @ApiOperation("通过提现申请")
    @PutMapping("/withdrawal/pass")
    public Result passWithdrawal(@ApiParam(name = "id", value = "工单ID", example = "0")
                                 @RequestParam(value = "id") Long id,
                                 @ApiParam(name = "tradeNo", value = "交易流水号，不是微信渠道时必填", example = "0")
                                 @RequestParam(value = "tradeNo", required = false) String tradeNo,
                                 @ApiParam(name = "channel", value = "结算类型 3->线下结算 只有当线下结算是参数才需要传递", example = "3")
                                 @RequestParam(value = "channel", required = false) Integer channel) {
        financeWithdrawalRecordService.passWithdrawal(id, tradeNo, channel);
        return Result.ok();
    }


    /**
     * Reject withdrawal result.
     *
     * @param id     the id
     * @param reason the reason
     * @return the result
     */
    @ApiOperation("拒绝提现申请")
    @PutMapping("/withdrawal/reject")
    public Result rejectWithdrawal(@ApiParam(name = "id", value = "工单ID", example = "0") @RequestParam(value = "id") Long id,
                                   @ApiParam(name = "reason", value = "拒绝说明", example = "账户异常") @RequestParam(value =
                                           "reason") String reason) {
        financeWithdrawalRecordService.rejectWithdrawal(id, reason);
        return Result.ok();
    }


    /**
     * Note withdrawal result.
     *
     * @param ids  the ids
     * @param over the over
     * @param note the note
     * @return the result
     */
    @ApiOperation(value = "批量备注订单")
    @PutMapping("/withdrawal/note")
    public Result noteWithdrawal(@RequestBody Long[] ids, @RequestParam(value = "over") Boolean over,
                                 @RequestParam(value = "note") String note) {
        financeWithdrawalRecordService.noteWithdrawal(Arrays.asList(ids), note, over);
        return Result.ok();
    }


}
